package peer.assist.message;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import peer.Config;
import peer.Message;
import peer.assist.AssistUser;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;

public class OtherOutAddressMessage2 extends Message{
	public short sType=peer.tools.MessageType.sType_Assist_OtherOutAddressMessage2;
	InetAddress nIp;
	byte[] dpData=new byte[8];
	InetAddress outIp;
	int outPortUDP;
	AssistUser au;
	int outPortTCP;
	InetAddress lanIp;int lanPortUDP;int lanPortTCP;
	int netType=Config.nodeType_PubNet;
	public OtherOutAddressMessage2(InetAddress outIp,int outPortUDP,int outPortTCP,InetAddress lanIp,int lanPortUDP,int lanPortTCP,int netType){
		byte[] dpData=new byte[32];
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		System.arraycopy(outIp.getAddress(), 0, dpData, 4, 4);
		ByteIntSwitch.toByteArray(outPortUDP, dpData, 8);
		ByteIntSwitch.toByteArray(outPortTCP, dpData, 12);
		System.arraycopy(lanIp.getAddress(), 0, dpData, 16, 4);
		ByteIntSwitch.toByteArray(lanPortUDP, dpData, 20);
		ByteIntSwitch.toByteArray(lanPortTCP, dpData, 24);
		ByteIntSwitch.toByteArray(netType, dpData, 28);
		dp=new DatagramPacket(dpData,dpData.length);
	}
	public OtherOutAddressMessage2(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		byte[] b=new byte[4];
		System.arraycopy(dpData, 4, b, 0, 4);
		try {
			outIp=InetAddress.getByAddress(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		outPortUDP=ByteIntSwitch.toInt(dpData, 8);
		outPortTCP=ByteIntSwitch.toInt(dpData, 12);

		System.arraycopy(dpData, 16, b, 0, 4);
		try {
			lanIp=InetAddress.getByAddress(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		lanPortUDP=ByteIntSwitch.toInt(dpData, 20);
		lanPortTCP=ByteIntSwitch.toInt(dpData, 24);
		netType=ByteIntSwitch.toInt(dpData, 28);
	
	}
	
	public InetAddress getOutIp(){
		return outIp;
	}
	
	public int getOutPortUDP(){
		return outPortUDP;
	}
	
	public int getOutPortTCP(){
		return outPortTCP;
	}
	
	public InetAddress getLanIp(){
		return lanIp;
	}
	
	public int getLanPortUDP(){
		return lanPortUDP;
	}
	
	public int getLanPortTCP(){
		return lanPortTCP;
	}
	
	public int getNetType(){
		return netType;
	}
}
